package com.limao.servlet;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.commons.codec.binary.Base64;

import com.limao.Tool.CacheClass;
import com.limao.Utils.OSSObjectSample;



@WebServlet(name="uploadImageBlob", urlPatterns="/uploadImageBlob",asyncSupported=true)
@MultipartConfig
public class uploadImageBlob extends HttpServlet {

	private static final long serialVersionUID = 9044147327011601498L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	
		processRequest(request,response);
		
	}
	protected void processRequest(HttpServletRequest request,
	        HttpServletResponse response)
	        throws ServletException, IOException {
		
		response.setContentType("text/html;charset=UTF-8");

	    final Part filePart = request.getPart("file");

	    OutputStream out = null;
	    InputStream filecontent = null;
	    final PrintWriter writer = response.getWriter();

	    switch(CacheClass.isOut)
		{
		case 2:
			try {
				final String fileName = getFileName(filePart);
				out = new FileOutputStream(new File("ss" + File.separator+ fileName));
				filecontent = filePart.getInputStream();
		        int read = 0;
		        final byte[] bytes = new byte[1024];

		        while ((read = filecontent.read(bytes)) != -1) {
		            out.write(bytes, 0, read);
		        }
		        
		        writer.print(CacheClass.getImgUrl()) ;
		        
			} catch (IOException ioe) {
				
			} finally {
		        if (out != null) {
		            out.close();
		        }
		        if (filecontent != null) {
		            filecontent.close();
		        }
		        if (writer != null) {
		            writer.close();
		        }
		    }
			
		case 0://正式环境 OSS
		case 1:
			try {

				String webFilePath=System.currentTimeMillis() + "_" + CacheClass.get6RandomCode()+".jpg";
				boolean falg;
				filecontent = filePart.getInputStream();
				//InputStream is = new ByteArrayInputStream(data);
				//外网上传图片
				falg =	OSSObjectSample.OSS_upload(filecontent, webFilePath, filePart.getSize(), filePart.getContentType());
				if(falg)
					writer.print(webFilePath) ;
				
            } catch (Exception e) {
             	e.printStackTrace();
        	} finally {
		        if (out != null) {
		            out.close();
		        }
		        if (filecontent != null) {
		            filecontent.close();
		        }
		        if (writer != null) {
		            writer.close();
		        }
		    }

		}
		
	}
	
	private String getFileName(final Part part) {
	    final String partHeader = part.getHeader("content-disposition");
	    for (String content : part.getHeader("content-disposition").split(";")) {
	        if (content.trim().startsWith("filename")) {
	            return content.substring(
	                    content.indexOf('=') + 1).trim().replace("\"", "");
	        }
	    }
	    return null;
	}
	
}
